//
//  YHMeSettlementItemCell.m
//  YHApp
//
//  Created by -- on 2021/7/27.
//

#import "HAMeSettlementItemCell.h"
#import "HAMeSettlementItemMo.h"

@interface HAMeSettlementItemCell ()

/***/
@property (nonatomic, strong) UILabel *nameL;
/***/
@property (nonatomic, strong) UIView *mainV;
/***/
@property (nonatomic, strong) UIView *lineView;
/***/
@property (nonatomic, strong) UILabel *exLabel;
/***/
@property (nonatomic, strong) UIButton *addButton;
/***/
@property (nonatomic, strong) UIButton *minusButton;
/***/
@property (nonatomic, strong) UILabel *numLabel;
/***/
@property (nonatomic, strong) UILabel *titLabel;


@end

@implementation HAMeSettlementItemCell

-(void)addButtonClick {
    
    float newValue = [_itemModel.value doubleValue]*10000;
    float max_trade = [_itemModel.max doubleValue]*10000;
    float step = [_itemModel.step doubleValue]*10000;

    newValue += step;
    if (newValue > max_trade) {
        [MBProgressHUD showError:@"超过最大值"];
    }else {
        //设置新值
        float valueFloat = (float)(newValue*0.0001);
        NSString *newValueString = [NSString stringWithFormat:@"%f",valueFloat];
        newValueString = [NSString changeFloat:newValueString];
        _itemModel.value = newValueString;
        self.numLabel.text = _itemModel.value;
    }
}

-(void)minusButtonClick {
    float newValue = [_itemModel.value floatValue]*10000;
    float min_trade = [_itemModel.min floatValue]*10000;
    float step = [_itemModel.step doubleValue]*10000;

    newValue -= step;
    if (newValue <  min_trade) {
        [MBProgressHUD showError:@"小于最小值"];
    }else {
        //设置新值
        float valueFloat = (float)(newValue*0.0001);
        NSString *newValueString = [NSString stringWithFormat:@"%f",valueFloat];
        newValueString = [NSString changeFloat:newValueString];
        _itemModel.value = newValueString;
        self.numLabel.text = _itemModel.value;
    }
}

-(void)setItemModel:(HAMeSettlementItemMo *)itemModel {
    _itemModel = itemModel;

    self.nameL.text = @"";//_itemModel.label;
    self.titLabel.text =  _itemModel.label;//@"本级扣留利润";
    self.numLabel.text = _itemModel.value;
    self.exLabel.text = [NSString stringWithFormat:@"设置范围%@~%@",_itemModel.min,_itemModel.max];
}


-(void)layoutSubviews {
    [super layoutSubviews];
    
    self.contentView.backgroundColor = [UIColor khaBGColor];
    
//    [self.nameL mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.left.mas_equalTo(20);
//        make.top.mas_equalTo(18);
//        make.height.mas_offset(18);
//    }];
    
    [self.mainV mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(0);
        make.right.mas_equalTo(0);
        make.top.mas_equalTo(20);
        make.bottom.mas_equalTo(0);
    }];
    
    [self.lineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(20);
        make.right.mas_equalTo(-20);
        make.height.mas_offset(0.8);
        make.top.mas_equalTo(54);
    }];
    
    [self.addButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(-20);
        make.top.mas_equalTo(13);
        make.width.mas_offset(28);
        make.height.mas_offset(28);
    }];
    
    [self.numLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(self.addButton.mas_left).offset(-4);
        make.centerY.mas_equalTo(self.addButton);
        make.width.mas_offset(80);
        make.height.mas_offset(28);
    }];
    
    [self.minusButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(self.numLabel.mas_left).offset(-4);
        make.centerY.mas_equalTo(self.addButton);
        make.width.mas_offset(28);
        make.height.mas_offset(28);
    }];

    [self.titLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(20);
        make.centerY.mas_equalTo(self.addButton);
        make.right.mas_equalTo(self.minusButton.mas_left).offset(-10);
    }];

    [self.exLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(20);
        make.top.mas_equalTo(self.lineView.mas_bottom).offset(7);
        make.right.mas_equalTo(-20);
    }];
}

- (UILabel *)nameL
{
    if (!_nameL) {
        _nameL = [UILabel ha_NLabelWithFont:17 textColor:@"#858b9c" text:@"" alignment:0];
        [self.contentView addSubview:_nameL];
    }
    return _nameL;
}

- (UIView *)mainV
{
    if (!_mainV) {
        _mainV = [UIView ha_createView];
        _mainV.backgroundColor  = [UIColor whiteColor];
        [self.contentView addSubview:_mainV];
    }
    return _mainV;
}

- (UIButton *)addButton
{
    if (!_addButton) {
        _addButton = [[UIButton alloc]init];
        [_addButton setTitle:@"+" forState:UIControlStateNormal];
        [_addButton setTitleColor:[UIColor colorWithHexString:@"#858B9C"] forState:UIControlStateNormal];
        _addButton.titleLabel.font = [UIFont systemFontOfSize:16];
        _addButton.backgroundColor = [UIColor colorWithHexString:@"#F9FAFB"];
        _addButton.layer.cornerRadius = 4;
        _addButton.layer.masksToBounds = YES;
        [_addButton addTarget:self action:@selector(addButtonClick) forControlEvents:UIControlEventTouchUpInside];
        [self.mainV addSubview:_addButton];
    }
    return _addButton;
}

- (UIButton *)minusButton
{
    if (!_minusButton) {
        _minusButton = [[UIButton alloc]init];
        [_minusButton setTitle:@"-" forState:UIControlStateNormal];
        [_minusButton setTitleColor:[UIColor colorWithHexString:@"#858B9C"] forState:UIControlStateNormal];
        _minusButton.titleLabel.font = [UIFont systemFontOfSize:16];
        _minusButton.backgroundColor = [UIColor colorWithHexString:@"#F9FAFB"];
        _minusButton.layer.cornerRadius = 4;
        _minusButton.layer.masksToBounds = YES;
        [_minusButton addTarget:self action:@selector(minusButtonClick) forControlEvents:UIControlEventTouchUpInside];
        [self.mainV addSubview:_minusButton];
    }
    return _minusButton;
}

- (UILabel *)numLabel
{
    if (!_numLabel) {
        _numLabel = [UILabel ha_NLabelWithFont:14 textColor:@"#858B9C" text:@"" alignment:1];
        _numLabel.backgroundColor = [UIColor colorWithHexString:@"#F9FAFB"];
        _numLabel.layer.cornerRadius = 4;
        _numLabel.layer.masksToBounds = YES;
        [self.mainV addSubview:_numLabel];
    }
    return _numLabel;
}

- (UILabel *)titLabel
{
    if (!_titLabel) {
        _titLabel = [UILabel ha_NLabelWithFont:18 textColor:@"#000000" text:@"" alignment:0];
        [self.mainV addSubview:_titLabel];
    }
    return _titLabel;
}

- (UIView *)lineView
{
    if (!_lineView) {
        _lineView = [UIView ha_createView];
        _lineView.backgroundColor = [UIColor kLineColor];
        [self.mainV addSubview:_lineView];
    }
    return _lineView;
}

- (UILabel *)exLabel
{
    if (!_exLabel) {
        _exLabel = [UILabel ha_NLabelWithFont:12 textColor:@"#858b9c" text:@"" alignment:0];
        [self.mainV addSubview:_exLabel];
    }
    return _exLabel;
}
@end
